package com.finger.crowd.mvc.controller;

import com.finger.crowd.entity.Menu;
import com.finger.crowd.service.api.MenuService;
import com.finger.crowd.util.ResultEntity;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class MenuController {
    @Autowired
    private MenuService menuService;

    private Gson gson = new Gson();

    //    @ResponseBody
//    @RequestMapping("/menu/get/whole/tree.json")
    // 厉害！
    public ResultEntity getWholeTreeOld() {
        // 获取全部的节点
        List<Menu> menus = menuService.getAll();
        // 先声明一个父节点
        Menu root = null;
        // 对全部的节点进行遍历
        for (Menu menu : menus) {
            // 获取id
            Integer pid = menu.getPid();
            // 查找父节点给父节点赋值
            if (menu.getPid() == null) {
                root = menu;
                continue;
            }
            // 遍历全部节点，为全部节点寻找父节点
            for (Menu menu1 : menus) {
                Integer id = menu1.getId();
                if (Objects.equals(pid, id)) {
                    menu1.getChildren().add(menu);
                    break;
                }
            }
        }
        return ResultEntity.successWithData(root);
    }


    @ResponseBody
    @RequestMapping("/menu/get/whole/tree.json")
    // 厉害!!!!!!!!!
    public String getWholeTreeNew() {
        // 从数据库取出全部数据
        List<Menu> menus = menuService.getAll();
        // 定义父节点
        Menu root = null;
        // 准备一个hashmap
        Map<Integer, Menu> map = new HashMap<>();
        // 以mune的id为key，menu为value，依次传入map
        for (Menu menu1 : menus) {
            Integer id = menu1.getId();
            map.put(id, menu1);
        }
        for (Menu menu1 : menus) {
            // 取出menu的id
            Integer pid = menu1.getPid();
            // 如果pid为null，就是父节点
            if (pid == null) {
                root = menu1;
                continue;
            }
            // 根据menu的id，从map里取出数据
            Menu father = map.get(pid);
            // 为当前节点赋值父节点
            father.getChildren().add(menu1);
        }
        Gson gson = new Gson();
        ResultEntity entity = ResultEntity.successWithData(root);
        String s = gson.toJson(entity);
        return s;
    }

    @RequestMapping("/save/to/node/page.json")
    @ResponseBody
    public String saveMenu(Menu menu) {
        int resultCounts = menuService.saveMenu(menu);
        if (resultCounts != 0) {
            ResultEntity<Object> resultEntity = ResultEntity.successWithOutData();
            String s = gson.toJson(resultEntity);
            return s;
        } else {
            ResultEntity<Object> result = ResultEntity.failed("请您重新操作");
            String s = gson.toJson(result);
            return s;
        }
    }

    @RequestMapping("menu/to/remove/page.json")
    @ResponseBody
    public String removeMenu(@RequestParam("id") Integer id) {
        int resultCounts = menuService.removeMenuByPrimaryKey(id);
        if (resultCounts != 0) {
            ResultEntity<Object> resultEntity = ResultEntity.successWithOutData();
            String s = gson.toJson(resultEntity);
            return s;
        } else {
            ResultEntity<Object> failed = ResultEntity.failed("操作失败");
            return gson.toJson(failed);
        }
    }

    @RequestMapping("/menu/to/edit.json")
    @ResponseBody
    public String editMenu(Menu menu) {
        int resultCounts = menuService.editMenu(menu);
        if (resultCounts != 0) {
            ResultEntity<Object> resultEntity = ResultEntity.successWithOutData();
            return gson.toJson(resultEntity);
        } else {
            ResultEntity<Object> resultEntity = ResultEntity.failed("操作失败");
            return gson.toJson(resultEntity);
        }
    }

}
